﻿using System.Text.Json;

namespace WXWorkSvc.Common.Extensions;
public enum ETempDataKey
{
    RateType,
    RateId,
    RateName,
    IsDelayPrice
}
public static class TempDataExtensions
{

    public static bool IsMobile(this ITempDataDictionary tempData) => tempData.ContainsKey("IsMobile");
    ///// <summary>
    ///// 第三方应用使用，获取当前CorpId
    ///// </summary>
    ///// <param name="tempData"></param>
    ///// <returns></returns>
    //public static string PeekCorpId(this ITempDataDictionary tempData) => tempData.Peek<string>("CorpId")!;
    ///// <summary>
    ///// 第三方应用使用，Corp的一个自增长Id
    ///// </summary>
    ///// <param name="tempData"></param>
    ///// <returns></returns>
    //public static int PeekExtId(this ITempDataDictionary tempData) => tempData.Peek<int>("ExtId");
    ///// <summary>
    ///// 内部应用使用，显示姓名等
    ///// </summary>
    ///// <param name="tempData"></param>
    ///// <returns></returns>
    //public static string? PeekUserName(this ITempDataDictionary tempData) => tempData.Peek<string>("UserName");
    ///// <summary>
    ///// 内部应用使用，UserItem的扩展属性
    ///// </summary>
    ///// <param name="tempData"></param>
    ///// <returns></returns>
    //public static string? PeekExt(this ITempDataDictionary tempData) => tempData.Peek<string>("Ext");

    public static T? Peek<T>(this ITempDataDictionary tempData, string key)
    {
        var obj = tempData.Peek(key);
        if (obj == null)
            return default;
        return obj.ToString()!.To<T>();
    }
    public static void Save<T>(this ITempDataDictionary tempData, string key, T value)
    {
        tempData[key] = value!.ToJsonString();
    }

    public static T? Peek<T>(this ITempDataDictionary tempData, ETempDataKey key)
    {
        var obj = tempData.Peek(key.ToString());
        if (obj == null)
            return default;
        return obj.ToString()!.To<T>();
    }
    public static void Save<T>(this ITempDataDictionary tempData, ETempDataKey key, T value)
    {
        tempData[key.ToString()] = value!.ToJsonString();
    }
}
